package spider

import (
	"fmt"
	"github.com/gocolly/colly"
	"gocolly/model"
)

func School(c *colly.Collector) {
	c.OnRequest(func(request *colly.Request) {

	})
	c.OnResponse(func(response *colly.Response) {
		fmt.Println(response.Request.URL)
	})
	c.OnError(func(response *colly.Response, err error) {
		if err != nil {
			panic("请求失败：" + err.Error())
		}
	})

	//高校
	c.OnHTML("#school", func(e *colly.HTMLElement) {
		e.ForEach("div", func(i int, element *colly.HTMLElement) {
			if i > 0 {
				var school model.School
				element.ForEach("span", func(j int, eSpan *colly.HTMLElement) {
					switch j {
					case 1:
						school.Name = eSpan.Text
						break
					case 2:
						school.Province = eSpan.Text
						break
					case 3:
						school.City = eSpan.Text
						break
					case 4:
						school.Level = eSpan.Text
						break
					case 5:
						school.IsPublic = eSpan.Text
						break
					case 6:
						school.Is985 = eSpan.Text
						break
					case 7:
						school.Is211 = eSpan.Text
						break
					case 8:
						school.IsDoubleNice = eSpan.Text
						break
					}
				})
				model.UpdateOrCreate(school)
			}
		})
	})

	c.Visit("http://tt.io/private.html")

}
